package com.itheima.sc.web.pms;


import com.itheima.sc.pojo.pms.ProductAttributeCategory;
import com.itheima.sc.result.Result;
import com.itheima.sc.service.pms.ProductAttributeCategoryService;
import com.itheima.sc.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("productAttribute/category")
public class ProductAttributeCategoryController {

    @Autowired
    private ProductAttributeCategoryService attributeCategoryService;

    @PostMapping("create")  //添加商品属性分类
    public Result create(@RequestParam String name){
        attributeCategoryService.create(name);
        return Result.success(null);
    }

    @GetMapping("delete/{id}")  //删除单个商品属性分类
    public Result delete(@PathVariable Integer id){
        attributeCategoryService.delete(id);
        return Result.success(null);
    }


    @GetMapping ("list")  //分页获取所有商品属性分类
    public Result list( @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        PageResult pageResult = attributeCategoryService.listByPage(pageNum,pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("list/withAttr") //获取所有商品属性分类及其下属性
    public Result listWithAttr(){
        List<ProductAttributeCategory> categoryList = attributeCategoryService.listWithAttr();
        return Result.success(categoryList);
    }

    @PostMapping("update/{id}")  //修改商品属性分类
    public Result updateById(@PathVariable Integer id,
                             @RequestParam String name){
        attributeCategoryService.updateById(id,name);
        return Result.success(null);
    }

    @GetMapping("{id}")  //获取单个商品属性分类信息
    public Result getById(@PathVariable Integer id){
        ProductAttributeCategory category = attributeCategoryService.getById(id);
        return Result.success(category);
    }


}
